package com.ywk.mapperRegister.core.scanRegister;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * @Author zbh
 * @Date 2023/5/9
 */
public class DBMapperScannerRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     * 批量注册 BeanDefinition
     * @param importingClassMetadata
     * @param registry
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        String path = importingClassMetadata.getAnnotationAttributes(DBMapperScan.class.getName()).get("value").toString();

        DBClassPathMapperScanner dbClassPathMapperScanner = new DBClassPathMapperScanner(registry);
        // 这里是因为 Spring 默认会排除一些文件，我们希望所有的都被扫描
        dbClassPathMapperScanner.addIncludeFilter(new TypeFilter() {
            @Override
            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                return true;
            }
        });
        //简洁写法
//        dbClassPathMapperScanner.addIncludeFilter((metadataReader, metadataReaderFactory)-> true);
        dbClassPathMapperScanner.scan(path);

    }
}
